/* t_base64.cpp : test dgnsc time
 * Copyright (C) 2011 drangon <drangon.zhou@gmail.com>
 * 2011-09
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <dgnsc/base64.h>

#include "gtest.h"

#include <stdio.h>
#include <assert.h>
#include <string.h>

TEST( base, encdec64 )
{
	const char * p1 = "hello world";
	const char * p2 = "aGVsbG8gd29ybGQ=";
	const char * p3 = "TPwJh>Io2Tv!lE";
	const char * p4 = "68656C6C6F20776F726C64";
	char buf[256];
	char buf2[256];
	int ret;

	ret = dgn_base64_enc( buf, p1, strlen(p1), -1 );
	EXPECT_EQ( ret, 16 );
	EXPECT_STREQ( buf, p2 );
	ret = dgn_base64_enc( buf, p1, strlen(p1), 4 );
	EXPECT_EQ( ret, 24 );
	EXPECT_STREQ( buf, "aGVs\r\nbG8g\r\nd29y\r\nbGQ=\r\n" );

	ret = dgn_base64_dec( buf2, buf, strlen(buf) );
	EXPECT_EQ( ret, 11 );
	EXPECT_STREQ( buf2, p1 );
	ret = dgn_base64_dec( buf, p2, strlen(p2) );
	EXPECT_EQ( ret, 11 );
	EXPECT_STREQ( buf, p1 );

	ret = dgn_base91_enc( buf, p1, strlen(p1) );
	EXPECT_EQ( ret, 14 );
	EXPECT_STREQ( buf, p3 );

	ret = dgn_base91_dec( buf, p3, strlen(p3) );
	EXPECT_EQ( ret, 11 );
	EXPECT_STREQ( buf, p1 );

	ret = dgn_base16_enc( buf, p1, strlen(p1) );
	// printf( "buf %s\n", buf );
	EXPECT_EQ( ret, 22 );
	EXPECT_STREQ( buf, p4 );

	ret = dgn_base16_dec( buf, p4, strlen(p4) );
	EXPECT_EQ( ret, 11 );
	EXPECT_STREQ( buf, p1 );
	
}

